﻿using System.Collections.Generic;
using System.Diagnostics;
using System.IO;

namespace OpenLS.Spreadsheet.Formats.Biff.Records
{
    internal class VerticalPageBreaksRecord : WorksheetLevelRecord
    {
        private readonly List<VertBrk> _rgbrk = new List<VertBrk>();

        internal override BiffRecordType RecordType
        {
            get { return BiffRecordType.VerticalPageBreaks; }
        }

        internal override void Apply(BiffReaderWorksheetContext context)
        {
            Debug.WriteLine("Applying vertical breaks");
        }

        internal override void WriteContent(BiffWriterWorkbookContext context)
        {
            ushort cbrk = (ushort) this._rgbrk.Count;
            context.Write(cbrk);
            ;
            foreach (var item in this._rgbrk)

                item.Write(context.Writer);
        }

        protected override void ReadContent(BiffReaderContext context, int length)
        {
            ushort cbrk = context.ReadUInt16();
            this._rgbrk.Clear();
            for (int i = 0; i < cbrk; i++)
            {
                VertBrk item = new VertBrk();
                item.Read(context.Reader);
                this._rgbrk.Add(item);
            }
        }
    }

    internal class VertBrk
    {
        private ushort col;
        private ushort rowStart;
        private ushort rowEnd;

        public void Read(BinaryReader reader)
        {
            this.col = reader.ReadUInt16();
            this.rowStart = reader.ReadUInt16();
            this.rowEnd = reader.ReadUInt16();
        }

        public void Write(BinaryWriter writer)
        {
            writer.Write(this.col);
            writer.Write(rowStart);
            writer.Write(rowEnd);
        }
    }
}